﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Pata.SylkSharp.Core.Bll;
using Pata.SylkSharp.Core.Utils;
using System.Text.RegularExpressions;
using System.Xml;

namespace Pata.SylkSharp.AppTestConsole
{
    class Program
    {
        /// <summary>
        /// Get a Record from a line of SylkFile
        /// </summary>
        /// <param name="pLine"></param>
        /// <returns></returns>
        private static IRecord GetRecord(ref string pLine)
        {
            Regex regex = new Regex(@"(?<type>(ID)|P|B|O|F|C|E){1}(;(?<restOfLine>.*;{0,1})){0,1}(?<lf>\n){0,1}");
            Match match = regex.Match(pLine);
            if (!match.Groups["type"].Success)
                throw new Exception(String.Format("Type could not be determined. Line content : {0}", pLine));
            else
            {
                IRecord record;
                Group restOfLineGroup = match.Groups["restOfLine"];
                switch (match.Groups["type"].Value.ToUpperInvariant())
                {
                    case "F":
                        record = new FRecord(restOfLineGroup.Value);
                        break;
                    case "ID":
                        record = new IDRecord(restOfLineGroup.Value);
                        break;
                    case "P":
                        record = new PRecord(restOfLineGroup.Value);
                        break;
                    case "O":
                        record = new ORecord(restOfLineGroup.Value);
                        break;
                    case "B":
                        record = new BRecord(restOfLineGroup.Value);
                        break;
                    case "C":
                        record = new CRecord(restOfLineGroup.Value);
                        break;
                    case "E":
                        record = new ERecord(restOfLineGroup.Value);
                        break;
                    default:
                        throw new NotImplementedException();
                }
                return record;
            }
        }

        static void Main(string[] args)
        {
            List<IRecord> recordList = new List<IRecord>();
            foreach (string filePath in new string[] { 
                ".\\Sample\\SampleData.slk" ,
                ".\\Sample\\SampleF.txt" })
            {
                using (FileStream fStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (StreamReader strReader = new StreamReader(fStream))
                    {
                        SylkFile slk = new SylkFile();

                        while (!strReader.EndOfStream)
                        {
                            string recordString = strReader.ReadLine();
                            IRecord iRecord = GetRecord(ref recordString);
                            if (iRecord != null)
                                recordList.Add(iRecord);
                        }
                    }
                }
            }

            Console.WriteLine("Total fields {0}", recordList.Select(r => r.Fields).Concat().Count());
            List<IField> unknowFieldList = recordList.Select(t => t.Fields.Where(f => f.Type.Equals(t.GetUnknownFieldType()))).Concat().ToList();
            Console.WriteLine("Unknown fields {0}", unknowFieldList.Count);
            foreach (IGrouping<Type, IField> fieldsByType in unknowFieldList.GroupBy(f => f.GetType()))
            {
                Console.WriteLine("For type {0} : {1}", fieldsByType.Key.FullName, fieldsByType.Count());
            }

            Console.ReadKey();
        }
    }
}
